home *** CD-ROM | disk | FTP | other *** search
-
- (function(namespace, $)
- {
- namespace.Preferences = function(prefsManager)
- {
- this.prefs = prefsManager;
- };
-
- namespace.Preferences.prototype = {
- prefs: null,
-
- init: function()
- {
- // setup interface event listeners
- this.setupEventListeners();
-
- // fill ui with data
- this.fillUI();
- },
-
- setupEventListeners: function()
- {
- // window listeners (to detect if any options were changed)
- $(window).bind('command.translator input.translator', function(e) {
- if($(e.originalTarget).hasClass('translator-preference')) {
- // enable apply button
- $('#translator-preferences-button-apply').boolean('disabled', false);
- }
- }.bind(this));
-
- // hotkey checkbox listener
- $('#translator-preferences-translate-hotkey').bind('command.translator', function(e) {
- $('#translator-preferences-hotkey, #translator-preferences-hotkey-note').boolean('disabled', (!$(e.target).boolean('checked')));
- });
-
- // hotkey textbox listeners
- $('#translator-preferences-hotkey')
- .bind('focus.translator', function(e) {
- e.target.select();
- })
- .bind('keypress.translator', function(e) {
- var modifiers = [];
- var hotkey = String.fromCharCode(e.which).toUpperCase();
-
- if(e.ctrlKey) {
- modifiers.push('control');
- }
- if(e.altKey) {
- modifiers.push('alt');
- }
- if(e.shiftKey) {
- modifiers.push('shift');
- }
- /*
- if(e.metaKey) {
- modifiers.push('meta');
- }
- */
- modifiers = modifiers.join(' ');
-
- $(e.target)
- .attr('hotkeymodifiers', modifiers)
- .attr('hotkey', hotkey)
- .val(this.formatHotKey(modifiers, hotkey));
-
- e.stopPropagation();
- }.bind(this));
-
-
- /* button listeners */
-
- // ok button
- $('#translator-preferences-button-ok').bind('command.translator', function(e) {
- this.savePreferences();
-
- window.close();
- }.bind(this));
-
- // apply button
- $('#translator-preferences-button-apply').bind('command.translator', function(e) {
- this.savePreferences();
-
- // disable apply button
- $('#translator-preferences-button-apply').boolean('disabled', true);
- }.bind(this));
-
- // cancel button
- $('#translator-preferences-button-cancel').bind('command.translator', function(e) {
- window.close();
- });
-
- // reset button
- $('#translator-preferences-button-reset').bind('command.translator', function(e) {
- // reset all preferences
- this.prefs.resetAllPrefs();
-
- // refill UI interface
- this.fillUI();
- }.bind(this));
- },
-
- fillUI: function()
- {
- // load list of all supported languages
- var $list = $('#translator-preferences-languages-list').eq(0).empty();
-
- $.each(namespace.Languages, function(code, name) {
- var $listitem = $(document.createElement('listitem'));
-
- $listitem.addClass('translator-preference');
-
- $listitem.attr('label', name);
- $listitem.attr('code', code);
- $listitem.attr('type', 'checkbox');
-
- $listitem.appendTo($list);
- }.bind(this));
-
-
- /* set all other preferences */
-
- $('#translator-preferences-translate-selection').boolean('checked', this.prefs.getPref('translate.selection'));
- $('#translator-preferences-translate-floating').boolean('checked', this.prefs.getPref('translate.floating'));
- $('#translator-preferences-translate-hotkey').boolean('checked', this.prefs.getPref('translate.hotkey'));
- $('#translator-preferences-status-icon').boolean('checked', this.prefs.getPref('status.icon'));
- $('#translator-preferences-status-label').boolean('checked', this.prefs.getPref('status.label'));
- $('#translator-preferences-toolbar').boolean('checked', this.prefs.getPref('toolbar'));
- $('#translator-preferences-context-menu').boolean('checked', this.prefs.getPref('context.menu'));
-
- // enable/disable hotkey textbox
- $('#translator-preferences-hotkey, #translator-preferences-hotkey-note').boolean('disabled', (!this.prefs.getPref('translate.hotkey')));
-
- // set hotkey
- $('#translator-preferences-hotkey')
- .val(this.formatHotKey(this.prefs.getPref('translate.hotkey.modifiers'), this.prefs.getPref('translate.hotkey.key')))
- .attr('hotkeymodifiers', this.prefs.getPref('translate.hotkey.modifiers'))
- .attr('hotkey', this.prefs.getPref('translate.hotkey.key'));
-
- // set selected languages
- $('#translator-preferences-languages-list').children().each(function(i, listitem) {
- if(this.prefs.getPref('languages.selected').search(new RegExp('(^|,)' + $(listitem).attr('code') + '(,|$)', 'i')) >= 0) {
- $(listitem).attr('checked', true);
- }
- }.bind(this));
-
- // disable apply button by default
- $('#translator-preferences-button-apply').boolean('disabled', true);
- },
-
- formatHotKey: function(modifiersString, keyString)
- {
- var keys = [];
- var modifiers = modifiersString.split(' ');
-
- $.each(modifiers, function(i, modifier) {
- switch(modifier) {
- case 'control':
- keys.push('Ctrl');
- break;
-
- case 'shift':
- keys.push('Shift');
- break;
-
- case 'alt':
- keys.push('Alt');
- break;
-
- case 'meta':
- keys.push('Meta');
- break;
- }
- });
-
- keys.push(keyString);
-
- return keys.join(' + ');
- },
-
- savePreferences: function(preferences)
- {
- var preferences = {};
-
- /* gather all preferences */
-
- // selected languages list
- preferences['languages.selected'] = [];
-
- $('#translator-preferences-languages-list').children().each(function(i, listitem) {
- if($(listitem).attr('checked') == 'true') {
- preferences['languages.selected'].push($(listitem).attr('code'));
- }
- });
-
- // all other preferences
- preferences['translate.selection'] = $('#translator-preferences-translate-selection').boolean('checked');
- preferences['translate.floating'] = $('#translator-preferences-translate-floating').boolean('checked');
- preferences['translate.hotkey'] = $('#translator-preferences-translate-hotkey').boolean('checked');
- preferences['translate.hotkey.modifiers'] = $('#translator-preferences-hotkey').attr('hotkeymodifiers');
- preferences['translate.hotkey.key'] = $('#translator-preferences-hotkey').attr('hotkey');
- preferences['status.icon'] = $('#translator-preferences-status-icon').boolean('checked');
- preferences['status.label'] = $('#translator-preferences-status-label').boolean('checked');
- preferences['toolbar'] = $('#translator-preferences-toolbar').boolean('checked');
- preferences['context.menu'] = $('#translator-preferences-context-menu').boolean('checked');
-
- // save preferences
- $.each(preferences, function(k, v) {
- this.prefs.setPref(k, v);
- }.bind(this));
- }
- };
- })(com.igorgladkov.translator, translatorJQuery);